const dbUtils = require('./../utils/db-util')

const user = {

  /**
   * 数据库创建用户
   * @param  {object} model 用户数据模型
   * @return {object}       mysql执行结果
   */
  async create ( model ) {
    let result = await dbUtils.insertData( 'users', model )
    return result
  },

  /**
   * 查找一个存在用户的数据
   * @param  {obejct} options 查找条件参数
   * @return {object|null}        查找结果
   */
  async getExistOne(options ) {
    let _sql = `
    SELECT * from users
      where stu_name="${options.username}" or stu_phone="${options.phone}"
      limit 1`
    let result = await dbUtils.query( _sql )
    if ( Array.isArray(result) && result.length > 0 ) {
      result = result[0]
    } else {
      result = null
    }
    return result
  },

  /**
   * 根据用户名和密码查找用户
   * @param  {object} options 用户名密码对象
   * @return {object|null}         查找结果
   */
  async getOneByUserNameAndPassword( options ) {
    let _sql = `
    SELECT * from users
      where password="${options.password}" and stu_name="${options.stu_name}"
      limit 1`
    let result = await dbUtils.query( _sql )
    if ( Array.isArray(result) && result.length > 0 ) {
      result = result[0]
    } else {
      result = null
    }
    return result
  },

  /**
   * 根据用户名查找用户信息
   * @param  {string} userName 用户账号名称
   * @return {object|null}     查找结果
   */
  async getUserInfoByUserName( userName ) {
    let _sql = `SELECT * from users where stu_name="${userName}"`
    let result = await dbUtils.query(_sql)
    if ( Array.isArray(result) && result.length > 0 ) {
      result = result[0]
    } else {
      result = null
    }
    return result
  },

    /**
   * 获取所有用户信息
   */
  async getUsers() {
    let _sql = `SELECT * from users `
    let result = await dbUtils.query(_sql)
    if (Array.isArray(result) && result.length > 0 ) {
      result = result
    } else {
      result = null
    }
    return result
  },

    /**
   * 获取所有用户信息
   */
  async delUsers(name) {
    let result = await dbUtils.deleteDataById('users', name)
    return result
  },
}


module.exports = user
